<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 17/4/26
 * Time: 15:13
 */

namespace Portal\Controller;


use Common\Controller\AdminbaseController;

class AdminManufacturerController extends AdminbaseController
{

    protected $main_model;
    protected $term_relationships_model;
    protected $terms_model;

    function _initialize() {
        parent::_initialize();
        $this->main_model = D("Portal/Manu");
        $this->manu_relationships_model = D("Portal/ManuRelation");
    }
    // 后台厂商管理列表
    public function index(){
        $this->_lists();
        $this->display();
    }
    public function add(){
        if(IS_POST){
            if($this->main_model->create()){
                if($id = $this->main_model->add()){
                    $extra = $_POST['extra'];
                    foreach ($extra as $k=>$v){
                        if(is_array($v)){
                            foreach ($v as $k1=>$v1){
                                if($v1)
                                $data[] = array(
                                    'attr_id'=>$v1,
                                    'manu_id'=>$id
                                );
                            }
                        }else{
                            if($v)
                            $data[] = array(
                                'attr_id'=>$v,
                                'manu_id'=>$id
                            );
                        }
                    }
                    $this->manu_relationships_model->addAll($data);
                }
                    $this->success('添加成功');
            }else{
                $this->error($this->main_model->getError());
            }
        }
        //属性字段
        $attr = $this->getAttr();
        //场馆
        $site = $this->getSite();
        $this->assign('site',$site);
        $this->assign('attr',$attr);
        $this->display();
    }
    public function edit(){
        if(IS_POST){
            $id = I('post.id');
            if($this->main_model->create()){
                    $this->main_model->save();
                    $oldExtra = $this->manu_relationships_model->where(array('manu_id'=>$id))->select();
                    $newData = array();
                    $oldData = array();
                    foreach ($oldExtra as $k=>$v) {
                        $oldData[] = $v['attr_id'];
                    }
                    $extra = $_POST['extra'];
                    foreach ($extra as $k=>$v){
                        if(is_array($v)){
                            foreach ($v as $k1=>$v1){
                                $newData[] = $v1;
                                if(!in_array($v1,$oldData)&&$v1){//判断是否已存在存在则不做重复更新
                                    $data[] = array(
                                        'attr_id'=>$v1,
                                        'manu_id'=>$id
                                    );
                                }
                            }
                        }else{
                            $newData[] = $v;
                            if(!in_array($v,$oldData)&&$v){
                                $data[] = array(
                                    'attr_id'=>$v,
                                    'manu_id'=>$id
                                );
                            }
                        }
                    }
                    $diff = array_diff($oldData,$newData);
                    if($diff)
                    $this->manu_relationships_model->where(array('manu_id'=>$id,'attr_id'=>array('in',$diff)))->delete();
                    if($data)
                        $this->manu_relationships_model->addAll($data);
                    $this->success('编辑成功');
            }else{
                $this->error($this->main_model->getError());
            }


        }
        $id = I('get.id');
        $info = $this->main_model->find($id);
        $extra = $this->manu_relationships_model->where(array('manu_id'=>$id))->select();
        $newExtra = array();
        foreach ($extra as $k=>$v){
            $newExtra[] = $v['attr_id'];
        }
        //属性字段
        $attr = $this->getAttr();
        //场馆
        $site = $this->getSite();
        $this->assign('site',$site);
        $this->assign('attr',$attr);
        $this->assign('info',$info);
        $this->assign('extra',$newExtra);
        $this->display();
    }
    public function test(){
        $str = "id=id\n模型名称=tableNam\n状态=status|fun\n操作=DEL[id[删除]]&EDIT[id[编辑]]";
        $arr = explode("\n",$str);
        $array = array();
        foreach ($arr as $k=>$v){
            $temp = explode('=',$v);
            $array[] = array(
                'table'=>$temp[0],
                'field'=>$temp[1]
            );
        }
        $s = array('id'=>2,'title'=>3);
        $rule = "/([A-Z]+)\[(\w+)\[([^\[\]]+)\]\]/";
        foreach ($array as $k=>$v){
            $temp_arr = array();
            preg_match_all($rule,$v['field'],$temp_arr);
            if($temp_arr[0]){
                $field = '';
                foreach ($temp_arr[0] as $k1=>$v1){
                    $field .= "<a href='".U(strtolower($temp_arr[1][$k1]),array($temp_arr[2][$k1]=>$s[$temp_arr[2][$k1]])).">{$temp_arr[3][$k1]}</a>";
                }
                $array[$k]['field'] = $field;
            }
        }
        dump($array);
        die;
    }
    /**
     * 文章列表处理方法,根据不同条件显示不同的列表
     * @param array $where 查询条件
     */
    private function _lists($where=array()){
        $status = I('request.status',-1);
        if($status>-1){
            $where['status'] = $status;
        }
        $is_access = I('request.is_access',-1);
        if($is_access>-1){
            $where['is_access'] = $is_access;
        }
        $keyword = I('keyword');
        if($keyword){
            $where['name'] = array('like','%'.$keyword.'%');//$keyword;
        }
        $count=$this->main_model->where($where)->count();
        $page = $this->page($count, 20);
        $posts=$this->main_model->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("listorder asc,id DESC")
            ->select();
        $this->assign('manu',C('MANU.STATUS'));
        $this->assign('is_access',C('MANU.IS_ACCESS'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("page", $page->show('Admin'));
        $this->assign("lists",$posts);
    }
    private function getAttr(){
        $parents = $this->getModule();
        foreach ($parents as $k => &$v) {
            $v['son'] = $this->getSon($v['id']);
        }
        $list = $parents;
        return $list;
    }
    public function listorders() {
        $status = parent::_listorders($this->main_model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }
    private function getModule(){
        return D('Common/ManuAttr')->select();
    }
    private function getSon($id){
        return D('Common/Attr')->where(array('module_id'=>$id))->select();
    }
    private function getSite(){
        return D('Common/Site')->select();
    }
}